from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
# Create your views here.
from django.views import View
from django_redis import get_redis_connection


class ImageCodeView(View):
    def get(self,request,uuid):
        from libs.captcha.captcha import captcha
        #内容和图片二进制
        text,image=captcha.generate_captcha()
        #保存
        from django_redis import get_redis_connection
        redis_cli = get_redis_connection('code')
        redis_cli.setex(uuid,200,text)
        #
        return HttpResponse(image,content_type='image/jpeg')


class SmsCodeView(View):
    def get(self,request,mobile):
        image_code = request.GET.get('image_code')
        uuid = request.GET.get('image_code_id')
        if not all([image_code,uuid]):
            return JsonResponse({'code':400,"errmsg":'参数不全'})

        redis_cli = get_redis_connection('code')
        redis_image_code = redis_cli.get(uuid)
        if redis_image_code is None:
            return JsonResponse({'code':400,'errmsg':'图片验证码已过期'})
        if redis_image_code.decode().lower() != image_code.lower():
            return JsonResponse({'code':400,'errmsg':'图片验证码错误'})
        #提取标记
        send_falg=redis_cli.get('send_flag_%s'%mobile)
        if send_falg is not None:
            return JsonResponse({'code':400,'errmsg':'不要频繁发送短信'})

        from random import randint
        sms_code = '%06d'%randint(0,999999)

        pipeline = redis_cli.pipeline()

        pipeline.setex(mobile,180,sms_code)
        pipeline.setex('send_flag_%s'%mobile,60,1)
        pipeline.execute()

        from libs.yuntongxun.sms import CCP
        # CCP().send_template_sms(mobile,[sms_code,3],1)
        
        from celery_tasks.sms.tasks import celery_send_sms_code
        celery_send_sms_code.delay(mobile,sms_code)
        return JsonResponse({'code':0,'errmsg':'ok'})
